    //
//  MainViewController.m
//  SpendFunk
//
//  Created by bm on 4/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MainViewController.h"
#import "SpendFunkAppDelegate.h"
#import "image.h"
#import "Global.h"
NSMutableArray *m_pArrFeed;
@implementation MainViewController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
-(id)init{
	return self;
}


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
	
	[self initView];
}

-(void)initView{
	NSString*path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_VIEW];
	UIImage *img = [UIImage imageNamed:path];
	UIImageView *imgvBkgr = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
	[imgvBkgr setImage:img];
	[self.view addSubview:imgvBkgr];
	[imgvBkgr setUserInteractionEnabled:YES];
	[imgvBkgr release];
	
	//viewContent
	m_pViewContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
	[m_pViewContent setBackgroundColor:[UIColor clearColor]];
	[m_pViewContent setUserInteractionEnabled:YES];
	[imgvBkgr addSubview:m_pViewContent];
	[m_pViewContent release];
	
	////
	path = [[NSString alloc] initWithFormat:@"%@",IMG_HEADER];
	img = [UIImage imageNamed:path];
	UIImageView *imgvBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
	[imgvBar setImage:img];
	[m_pViewContent addSubview:imgvBar];
	[imgvBar setUserInteractionEnabled:YES];
	[imgvBar release];
	///
//bottom bar
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_BOTTOM_BKGR];
	img = [UIImage imageNamed:path];
	UIImageView *imgvBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0, 480-img.size.height -19, img.size.width, img.size.height)];
	[imgvBottom setImage:img];
	[self.view addSubview:imgvBottom];
	[imgvBottom setUserInteractionEnabled:YES];
	[imgvBottom release];
	
//item
	//feed
	UILabel *pLblFeed = [[UILabel alloc] initWithFrame:CGRectMake(36, 30, 80, 30)];
	pLblFeed.text = @"FEED";
	pLblFeed.textColor = [UIColor yellowColor];
	[pLblFeed setBackgroundColor:[UIColor clearColor]];
	pLblFeed.font = [UIFont fontWithName:@"Arial" size:10];
	[imgvBottom addSubview:pLblFeed];
	[pLblFeed release];
	
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_FEED];
	img = [UIImage imageNamed:path];
	UIImageView *imgvFeed = [[UIImageView alloc] initWithFrame:CGRectMake(40, 35-img.size.height, img.size.width, img.size.height)];
	[imgvFeed setBackgroundColor:[UIColor clearColor]];
	[imgvFeed setImage:img];
	[imgvFeed setUserInteractionEnabled:YES];
	[imgvBottom addSubview:imgvFeed];
	[imgvFeed release];
	m_pBtnFeed = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 105, 100)];
	[m_pBtnFeed setBackgroundColor:[UIColor clearColor]];
	[m_pBtnFeed addTarget:nil action:@selector(onClickFeed) forControlEvents:UIControlEventTouchUpInside];
	[imgvBottom addSubview:m_pBtnFeed];
	
	//post
	UILabel *pLblPost = [[UILabel alloc] initWithFrame:CGRectMake(145, 30, 80, 30)];
	pLblPost.text = @"POST";
	pLblPost.textColor = [UIColor yellowColor];
	[pLblPost setBackgroundColor:[UIColor clearColor]];
	pLblPost.font = [UIFont fontWithName:@"Arial" size:10];
	[imgvBottom addSubview:pLblPost];
	[pLblPost release];
	
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_POST];
	img = [UIImage imageNamed:path];
	UIImageView *imgvPost = [[UIImageView alloc] initWithFrame:CGRectMake(138, 35-img.size.height, img.size.width, img.size.height)];
	[imgvPost setBackgroundColor:[UIColor clearColor]];
	[imgvPost setImage:img];
	[imgvBottom addSubview:imgvPost];
	[imgvPost setUserInteractionEnabled:YES];
	[imgvPost release];
	m_pBtnPost = [[UIButton alloc] initWithFrame:CGRectMake(105, 0, 110, 100)];
	[m_pBtnPost setBackgroundColor:[UIColor clearColor]];
	[m_pBtnPost addTarget:nil action:@selector(onClickPost) forControlEvents:UIControlEventTouchUpInside];
	[imgvBottom addSubview:m_pBtnPost];
	
	//following
	UILabel *pLblFollower = [[UILabel alloc] initWithFrame:CGRectMake(240, 30, 120, 30)];
	pLblFollower.text = @"FOLLOWING";
	pLblFollower.textColor = [UIColor yellowColor];
	[pLblFollower setBackgroundColor:[UIColor clearColor]];
	pLblFollower.font = [UIFont fontWithName:@"Arial" size:10];
	[imgvBottom addSubview:pLblFollower];
	[pLblFollower release];
	
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_FOLLOWER];
	img = [UIImage imageNamed:path];
	UIImageView *imgvFollowing = [[UIImageView alloc] initWithFrame:CGRectMake(260, 35-img.size.height, img.size.width, img.size.height)];
	[imgvFollowing setBackgroundColor:[UIColor clearColor]];
	[imgvFollowing setImage:img];
	[imgvBottom addSubview:imgvFollowing];
	[imgvFollowing setUserInteractionEnabled:YES];
	[imgvFollowing release];
	m_pBtnFollowing = [[UIButton alloc] initWithFrame:CGRectMake(215, 0, 105, 100)];
	[m_pBtnFollowing setBackgroundColor:[UIColor clearColor]];
	[m_pBtnFollowing addTarget:nil action:@selector(onClickFollowing) forControlEvents:UIControlEventTouchUpInside];
	[imgvBottom addSubview:m_pBtnFollowing];
	// Please wait ... 
	_blockerView = [[[UIView alloc] initWithFrame: CGRectMake(0, 0, 200, 60)] autorelease];
	_blockerView.backgroundColor = [UIColor colorWithWhite: 0.0 alpha: 0.8];
	_blockerView.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
	_blockerView.alpha = 0.0;
	_blockerView.clipsToBounds = YES;
	if ([_blockerView.layer respondsToSelector: @selector(setCornerRadius:)]) [(id) _blockerView.layer setCornerRadius: 10];
	
	UILabel	*label = [[[UILabel alloc] initWithFrame: CGRectMake(0, 5, _blockerView.bounds.size.width, 15)] autorelease];
	label.text = NSLocalizedString(@"Please Wait…", nil);
	label.backgroundColor = [UIColor clearColor];
	label.textColor = [UIColor whiteColor];
	label.textAlignment = UITextAlignmentCenter;
	label.font = [UIFont boldSystemFontOfSize: 15];
	[_blockerView addSubview: label];
	
	UIActivityIndicatorView				*spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite] autorelease];
	
	spinner.center = CGPointMake(_blockerView.bounds.size.width / 2, _blockerView.bounds.size.height / 2 + 10);
	[_blockerView addSubview: spinner];
	[self.view addSubview: _blockerView];
	[spinner startAnimating];
}

-(void)displayFeaturedMemberWithMembers:(NSMutableArray	*)members{
	if(!m_pFeaturedMemberCtrl){
		m_pFeaturedMemberCtrl = [[FeatureMembersController alloc] init];
	}
	[m_pFeaturedMemberCtrl setArrayMembers:members];
	[m_pViewContent addSubview:m_pFeaturedMemberCtrl.view];
}

#pragma mark function

-(void)openHighView{
	if(!m_pHighCtrl){
		m_pHighCtrl = [[HighRollerViewController alloc] init];
	}
	[m_pViewContent addSubview:m_pHighCtrl.view];
	animateView(m_pViewContent, kCATransitionPush, kCATransitionFromRight, 0.3);
}

-(void)onClickPost{
	if(!m_pPost1VCtrl){
		m_pPost1VCtrl = [[Post1ViewController alloc] init];
	}
	//if (m_pPost1VCtrl) {
//		[m_pPost1VCtrl release];
//		m_pPost1VCtrl =nil;
//	}
//	if (m_pFeedCtrl) {
//		[m_pFeedCtrl	 release];
//		m_pFeedCtrl = nil;
//	}
	[m_pViewContent addSubview:m_pPost1VCtrl.view];
	animateView(m_pViewContent, kCATransitionPush, kCATransitionFromRight, 0.3);
}

-(void)backToPost1{
	if(!m_pPost1VCtrl){
		m_pPost1VCtrl = [[Post1ViewController alloc] init];
	}
	[m_pViewContent addSubview:m_pPost1VCtrl.view];
	animateView(m_pViewContent, kCATransitionPush, kCATransitionFromLeft, 0.3);
}

-(void)onClickToPost2{
	if(!m_pPost2Crl)
	m_pPost2Crl =  [[Post2ViewController alloc] init];
	
	[m_pViewContent addSubview:m_pPost2Crl.view];
	animateView(m_pViewContent, kCATransitionPush, kCATransitionFromRight, 0.3);
}

- (void)openPriceView{
	if (!m_pPriceCtrl) {
		m_pPriceCtrl = [[PriceViewController alloc] init];
	}
	[m_pViewContent addSubview:m_pPriceCtrl.view];
	animateView(m_pViewContent, kCATransitionPush, kCATransitionFromRight, 0.3);
}
- (void)openWhereViewWithArrPaces:(NSArray*)arrPlaces{
	if (!m_pWhereCtrl) {
		m_pWhereCtrl = [[WhereViewController alloc] init];
	}
	[m_pWhereCtrl setArrPlaces:arrPlaces];
	[m_pViewContent addSubview:m_pWhereCtrl.view];
	animateView(m_pViewContent, kCATransitionPush, kCATransitionFromRight, 0.3);
}
- (void)openCateWithArrCate:(NSArray*)arrCate{
	if (!m_pCateCtrl) {
		m_pCateCtrl = [[CategoryViewController alloc] init];
	}
	[m_pCateCtrl setArrCate:arrCate];
	[m_pViewContent addSubview:m_pCateCtrl.view];
	animateView(m_pViewContent, kCATransitionPush, kCATransitionFromRight, 0.3);
}


-(void)openBadgesView{
	if(!m_pBadgesCtrl){
		m_pBadgesCtrl = [[BadgesViewController alloc] init];
	}
	
	[m_pViewContent addSubview:m_pBadgesCtrl.view];
	animateView(m_pViewContent, kCATransitionPush, kCATransitionFromRight, 0.3);
}

-(void)openLocationViewwithFS_ID:(NSString*)fsquareID gLat:(NSString*)gLat gLong:(NSString*)gLong aMount:(NSString*)amount Address:(NSString*)Address City:(NSString*)city{
	if(!m_pLocationCtrl){
		m_pLocationCtrl = [[LocationViewController alloc] init];
	}
	[m_pViewContent addSubview:m_pLocationCtrl.view];
	animateView(m_pViewContent, kCATransitionPush, kCATransitionFromRight, 0.3);
	[m_pLocationCtrl setFS_ID:fsquareID gLat:gLat gLong:gLong aMount:amount Address:Address City:city];
}

-(void)openUserProfile:(NSDictionary *)userProfile{
	if(!m_pUserProfileCtrl){
		m_pUserProfileCtrl = [[UserProfileController alloc] init];
	}
	[m_pViewContent addSubview:m_pUserProfileCtrl.view];
	animateView(m_pViewContent, kCATransitionPush, kCATransitionFromRight, 0.3);
	[m_pUserProfileCtrl setUserProfile:[userProfile retain]];
}
- (void)openCommentView:(NSString*)spendID{
	if (!m_pCommentCtrl) {
		m_pCommentCtrl = [[CommentViewController alloc] init];
	}
	[m_pCommentCtrl setSpendID:spendID];
	[m_pViewContent addSubview:m_pCommentCtrl.view];
	animateView(m_pViewContent, kCATransitionPush, kCATransitionFromRight, 0.3);
}

#pragma mark Event when click on Post
- (void)onBackCliclFromPost1{
	
}
- (void)onNextClickFromPost1{
	
}
- (void)onBackCliclFromPost2{
	
}
- (void)onDoneClickFromPost2{
	
}
- (void)onAddImgClickFromPost2{
	
}
#pragma mark Event when click on Feed

-(void)onClickFeed{
	//if (m_pFeedCtrl) {
//		return;
//	}
	//if (m_pPost1VCtrl) {
//		[m_pPost1VCtrl release];
//		m_pPost1VCtrl = nil;
//	}
//	if (m_pFeaturedMemberCtrl) {
//		[m_pFeaturedMemberCtrl release];
//		m_pFeaturedMemberCtrl =nil;
//	}
	_blockerView.alpha =1.0;
	[self getAllFollowingSpend];
	
}

-(void)openFeedView{
	[m_pViewContent addSubview:m_pFeedCtrl.view];
	animateView(m_pViewContent, kCATransitionPush, kCATransitionFromLeft, 0.3);
}

#pragma mark Event when click on Following
-(void)onClickFollowing{
	//if (m_pFeaturedMemberCtrl) {
//		return;
//	}
	//if (m_pFeedCtrl) {
//		[m_pFeedCtrl release];
//		m_pFeedCtrl = nil;
//	}
//	if (m_pPost1VCtrl) {
//		[m_pFeedCtrl	 release];
//		m_pPost1VCtrl =nil;
//	}
	if(!m_pFeaturedMemberCtrl){
		m_pFeaturedMemberCtrl = [[FeatureMembersController alloc] init];
	}
	//[m_pFeaturedMemberCtrl setArrayMembers:m_pArrMembers];
	[m_pViewContent addSubview:m_pFeaturedMemberCtrl.view];
	animateView(m_pViewContent, kCATransitionPush, kCATransitionFromRight, 0.3);
}

// API 8 get all following spend
#pragma mark getAllFollowingSpend
-(void)getAllFollowingSpend{
	NSString *limit = @"1";
	NSString *offset = @"0";
	NSString *post = [NSString stringWithFormat:@"userid=%@&limit=%@&offset=%@",g_strLoginUserId,limit,offset];
	NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
	NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
	NSURL *url = [NSURL URLWithString:@"http://demo.thejupitech.com/snt/spendfunk/api/spend/getallfollowingspend"];
	
	NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
	[theRequest setHTTPMethod:@"POST"];  
	[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];  
	[theRequest setHTTPBody:postData];	
	
	NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
	if( theConnection )
	{
		m_pWebData = [[NSMutableData data] retain];
		isContinueParse = true;
		if(!m_pArrFeed){
			m_pArrFeed = [[NSMutableArray alloc] init];
		}else {
			[m_pArrFeed removeAllObjects];
		}
	}
	else
	{
		
	}
}

#pragma mark delegate connection
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
	[m_pWebData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
	[m_pWebData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot connect to internet" message:@"Please connect to internet!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[alert show];
	
	[alert release];
	[connection release];
	[m_pWebData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
	NSData *loginStatus = [[NSData alloc] initWithBytes: [m_pWebData mutableBytes] length:[m_pWebData length]];
	
	NSString *a = [[NSString alloc] initWithData:loginStatus encoding:NSUTF8StringEncoding];
	NSLog(@"%@",a);
	
	NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:loginStatus];
	[xmlParser setDelegate:self];
	
	BOOL success = [xmlParser parse];
	
	if(success)
	{
		_blockerView.alpha =0.0;
		NSLog(@"No Errors");
		if (!m_pFeedCtrl) {
			m_pFeedCtrl = [[FeedController alloc] init];
		}
		[m_pFeedCtrl setArrFeed:m_pArrFeed];
		[m_pViewContent addSubview:m_pFeedCtrl.view];
		animateView(m_pViewContent, kCATransitionPush, kCATransitionFromRight, 0.3);
	}
	else
	{
		NSLog(@"Error Error Error!!!");
	}
	
	[connection release];
	[m_pWebData release];
}

#pragma mark delegate xmlParser
//parse xong se phai tra du lieu cho mang m_pArrContent
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
	attributes:(NSDictionary *)attributeDict {
	if(!currentElement) 
		currentElement = [[NSString alloc] initWithString:elementName];
	else
		currentElement = [elementName retain];
	
	NSLog(@"Processing Element: %@", elementName);
	//iCurrCount = iCountFriend;
	if([currentElement isEqualToString:kFollowID]){
		iCountFeed++;
		if(iCountFeed >=2){
			[m_pArrFeed addObject:m_pDicFeedInfo];
		}
		[m_pDicFeedInfo release];
		m_pDicFeedInfo = nil;
		m_pDicFeedInfo = [[NSMutableDictionary alloc] init];
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	NSLog(@"Processing Value: %@", string);
	
	
	// title
	if([currentElement isEqualToString:kTitle]){
		[m_pDicFeedInfo setObject:string forKey:kTitle];
	}
	// Adreess
	if([currentElement isEqualToString:kAddress]){
		[m_pDicFeedInfo setObject:string forKey:kAddress];
	}
	if([currentElement isEqualToString:kCity]){
		[m_pDicFeedInfo setObject:string forKey:kCity];
	}
	// picture
	if([currentElement isEqualToString:kPicture]){
		[m_pDicFeedInfo setObject:string forKey:kPicture];
	}
	// Details
	if([currentElement isEqualToString:kDetails]){
		[m_pDicFeedInfo setObject:string forKey:kDetails];
	}
	// Follow id
	if([currentElement isEqualToString:kFollowID]){
		[m_pDicFeedInfo setObject:string forKey:kFollowID];
	}
	
	// userid_fr
	if([currentElement isEqualToString:kUserID_fr]){
		[m_pDicFeedInfo setObject:string forKey:kUserID_fr];
	}
	// userid_to
	if([currentElement isEqualToString:kUserID_to]){
		[m_pDicFeedInfo setObject:string forKey:kUserID_to];
	}
	// get lat
	if([currentElement isEqualToString:kLat]){
		[m_pDicFeedInfo setObject:string forKey:kLat];
	}
	// geo long
	if([currentElement isEqualToString:kLong]){
		[m_pDicFeedInfo setObject:string forKey:kLong];
	}
	// Picture 
	if([currentElement isEqualToString:kPicture]){
		[m_pDicFeedInfo setObject:string forKey:kPicture];
	}
	// foursquare id
	if([currentElement isEqualToString:kFSquareID]){
		[m_pDicFeedInfo setObject:string forKey:kFSquareID];
	}
	// start 
	if([currentElement isEqualToString:kStart]){
		[m_pDicFeedInfo setObject:string forKey:kStart];
	}
	// spendid
	if([currentElement isEqualToString:kSpendID]){
		[m_pDicFeedInfo setObject:string forKey:kSpendID];
	}
	// amount
	if([currentElement isEqualToString:kAmount]){
		[m_pDicFeedInfo setObject:string forKey:kAmount];
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	
	[currentElement release];
	currentElement = nil;
	[currentElementValue release];
	currentElementValue = nil;
	if([elementName isEqualToString:@"xml"]){
		if (iCountFeed == 1) {
			[m_pArrFeed addObject:m_pDicFeedInfo];
			[m_pArrFeed retain];
		}
		//[m_pMainTableView reloadData];
	}
	
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
